﻿using System;
using System.Collections.Generic;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace IdPhotoAPIClient
{
    public class IdPhotoHelper
    {
        private IdPhotoApiClient _client;
        private string _timeString = DateTime.Now.ToString("yyMMdd_HHmmssfff");
        private string _imagePath = "test.jpg";


        private string fileName_standard = ""; // 标准照(底透明)
        private string fileName_hd = ""; // 高清照(底透明)
        private string fileName_background = ""; // 带背景照
        private string fileName_layout = ""; // 排板照 , 两排共10张
        private string fileName_watermarked = ""; // 水印照

        public IdPhotoHelper(IdPhotoApiClient client, string imagePath = "test.jpg")
        {
            _client = client;
            _timeString = DateTime.Now.ToString("yyMMdd_HHmmssfff");
            _imagePath = imagePath;

            fileName_standard = _timeString + "_standard.jpg";
            fileName_hd = _timeString + "_hd.jpg";
            fileName_background = _timeString + "_with_background.jpg";
            fileName_layout = _timeString + "_layout.jpg";
            fileName_watermarked = _timeString + "_watermarked.jpg";
        }

        public async Task<string> CreateIdPhoto(string color = "638cce")
        {
            try
            {
                await DemoIdPhoto(); // 1. 生成证件照(底透明)
                await DemoAddBackground(color); // 2,添加背景
                return fileName_background;
            }
            catch (Exception ex)
            {
                Console.WriteLine($"发生错误: {ex.Message}");
            }
            return "";
        }

        public async Task<string> CreateIdPhotoWatermark(string color = "638cce")
        {
            try
            {
                await CreateIdPhoto(color);
                await DemoWatermark(); // 添加水印
                return fileName_watermarked;
            }
            catch (Exception ex)
            {
                Console.WriteLine($"发生错误: {ex.Message}");
            }
            return "";
        }

        /// <summary>
        /// 生成证件照(底透明)
        /// </summary>
        /// <returns></returns>
        public async Task<string> DemoIdPhoto()
        {
            Console.WriteLine("\n=== 1. 生成证件照(底透明) ===");
            try
            {
                if (File.Exists(_imagePath))
                {
                    var result = await _client.IdPhotoAsync(_imagePath, 413, 295);
                    Console.WriteLine($"状态: {result.status}");
                    if (result.status && result.image_base64_standard != null)
                    {

                        // 保存结果图片
                        var base64String = result.image_base64_standard;
                        await SaveBase64Image(base64String, fileName_standard);
                        if (!string.IsNullOrEmpty(result.image_base64_hd))
                        {
                            var base64hd = result.image_base64_hd;
                            await SaveBase64Image(base64hd, fileName_hd);
                        }
                        Console.WriteLine("证件照生成成功，已保存到本地文件。");
                        return fileName_standard;
                    }
                }
                else
                {
                    Console.WriteLine($"跳过测试：未找到测试图片 {_imagePath}");
                }
            }
            catch (Exception ex)
            {
                Console.WriteLine($"生成证件照失败: {ex.Message}");
            }
            return "";
        }

        /// <summary>
        /// 添加背景色
        /// </summary>
        /// <returns>图片地址</returns>
        public async Task<string> DemoAddBackground(string color = "638cce")
        {
            Console.WriteLine("\n=== 2. 添加背景色 ===");
            try
            {
                if (File.Exists(fileName_standard))
                {
                    var result = await _client.AddBackgroundAsync(fileName_standard, color);
                    Console.WriteLine($"状态: {result.status}");
                    if (result.status && result.image_base64 != null)
                    {
                        var base64hd = result.image_base64;
                        await SaveBase64Image(base64hd, fileName_background);
                        Console.WriteLine("添加背景色成功，已保存到本地文件。");
                        return fileName_background;
                    }
                }
                else
                {
                    Console.WriteLine($"跳过测试：未找到测试图片 {fileName_standard}");
                }
            }
            catch (Exception ex)
            {
                Console.WriteLine($"添加背景色失败: {ex.Message}");
            }
            return "";
        }

        /// <summary>
        /// 图像加水印, 给 fileName_background 加水印
        /// </summary>
        private async Task DemoWatermark()
        {
            Console.WriteLine("\n=== 5. 图像加水印 ===");
            try
            {
                if (File.Exists(fileName_background))
                {
                    var result = await _client.WatermarkAsync(fileName_background, "Test Photo");
                    Console.WriteLine($"状态: {result.status}");
                    if (result.status && result.image_base64 != null)
                    {
                        await SaveBase64Image(result.image_base64, fileName_watermarked);
                        Console.WriteLine("添加水印成功，已保存到本地文件。");
                    }
                }
                else
                {
                    Console.WriteLine($"跳过测试：未找到测试图片 {fileName_background}");
                }
            }
            catch (Exception ex)
            {
                Console.WriteLine($"添加水印失败: {ex.Message}");
            }
        }

        /// <summary>
        /// 生成六寸排版照
        /// </summary>
        public async Task DemoGenerateLayoutPhotos()
        {
            Console.WriteLine("\n=== 3. 生成六寸排版照 ===");
            try
            {
                if (File.Exists(fileName_background))
                {
                    var result = await _client.GenerateLayoutPhotosAsync(fileName_background, 413, 295);
                    Console.WriteLine($"状态: {result.status}");
                    if (result.status && result.image_base64 != null)
                    {
                        await SaveBase64Image(result.image_base64, fileName_layout);
                        Console.WriteLine("六寸排版照生成成功，已保存到本地文件。");
                    }
                }
                else
                {
                    Console.WriteLine($"跳过测试：未找到测试图片 {fileName_background}");
                }
            }
            catch (Exception ex)
            {
                Console.WriteLine($"生成六寸排版照失败: {ex.Message}");
            }
        }


        /// <summary>
        /// 人像抠图
        /// </summary>
        /// <param name="imagePath"></param>
        /// <returns></returns>
        public async Task DemoHumanMatting()
        {
            Console.WriteLine("\n=== 4. 人像抠图 ===");
            try
            {
                if (File.Exists(_imagePath))
                {
                    var result = await _client.HumanMattingAsync(_imagePath);
                    Console.WriteLine($"状态: {result.status}");
                    if (result.status && result.image_base64 != null)
                    {
                        await SaveBase64Image(result.image_base64, _timeString + "_matting.png");
                        Console.WriteLine("人像抠图成功，已保存到本地文件。");
                    }
                }
                else
                {
                    Console.WriteLine($"跳过测试：未找到测试图片 {_imagePath}");
                }
            }
            catch (Exception ex)
            {
                Console.WriteLine($"人像抠图失败: {ex.Message}");
            }
        }

        /// <summary>
        /// 设置图像KB大小
        /// </summary>
        public async Task DemoSetKb()
        {
            Console.WriteLine("\n=== 6. 设置图像KB大小 ===");
            try
            {
                if (File.Exists(_imagePath))
                {
                    var result = await _client.SetKbAsync(_imagePath, 50); // 设置为50KB
                    Console.WriteLine($"状态: {result.status}");
                    if (result.status && result.image_base64 != null)
                    {
                        await SaveBase64Image(result.image_base64, _timeString + "_kb_adjusted_image.jpg");
                        Console.WriteLine("设置图像KB大小成功，已保存到本地文件。");
                    }
                }
                else
                {
                    Console.WriteLine($"跳过测试：未找到测试图片 {_imagePath}");
                }
            }
            catch (Exception ex)
            {
                Console.WriteLine($"设置图像KB大小失败: {ex.Message}");
            }
        }

        /// <summary>
        /// 证件照裁切, 使用已有的证件照底透明图片裁切成需要的尺寸
        /// </summary>
        public async Task DemoIdPhotoCrop()
        {
            Console.WriteLine("\n=== 7. 证件照裁切 ===");
            try
            {
                if (File.Exists(_imagePath))
                {
                    var result = await _client.IdPhotoCropAsync(_imagePath, 413, 295);
                    Console.WriteLine($"状态: {result.status}");
                    if (result.status)
                    {
                        if (!string.IsNullOrEmpty(result.image_base64_standard))
                        {
                            await SaveBase64Image(result.image_base64_standard, _timeString + "_cropped_idphoto_standard.jpg");
                            Console.WriteLine("证件照裁切成功，已保存到本地文件。");
                        }
                        if (!string.IsNullOrEmpty(result.image_base64_hd))
                        {
                            await SaveBase64Image(result.image_base64_hd, _timeString + "_cropped_idphoto_hd.jpg");
                        }
                    }
                }
                else
                {
                    Console.WriteLine($"跳过测试：未找到测试图片 {_imagePath}");
                }
            }
            catch (Exception ex)
            {
                Console.WriteLine($"证件照裁切失败: {ex.Message}");
            }
        }

        /// <summary>
        /// 把Base64字符串保存为图片文件
        /// </summary>
        /// <param name="base64String"></param>
        /// <param name="fileName"></param>
        public async Task SaveBase64Image(string? base64String, string fileName)
        {
            if (string.IsNullOrEmpty(base64String)) return;

            var imageBytes = Convert.FromBase64String(base64String.Replace("data:image/png;base64,", ""));
            await File.WriteAllBytesAsync(fileName, imageBytes);
        }
    }
}
